package cn.donzell.app.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.map.MapUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.donzell.app.model.Identity;

public class ExcelUtils {

	public static void createExcel(List<Identity> datas, String filePath)  {
		StopWatch stopWatch=new StopWatch();
		stopWatch.start();
		Map<String, String> headerMap = MapUtil.newHashMap(true);
		headerMap.put("name", "姓名");
		headerMap.put("idCard", "身份证号");
		headerMap.put("birthday", "生日");
		headerMap.put("sex", "性别");
		headerMap.put("area", "地区");

		BigExcelWriter writer= new BigExcelWriter();
		//合并列，设置表格名称
		//writer.merge(7, "合并的表头");
		//设置标题别名，有序输出
		writer.setHeaderAlias(headerMap);
		writer.writeHeadRow(headerMap.values());
		IntStream.range(0,5).forEach(p-> writer.setColumnWidth(p,25));
		writer.write(datas, false);
		try(FileOutputStream fos = new FileOutputStream(filePath)) {
			writer.flush(fos);
			writer.close();
			stopWatch.stop();
			System.out.println("花费"+stopWatch.getTotalTimeSeconds()+"秒");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readData(String filePath) {
		List<String> datas = new ArrayList<String>();
		try {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(System.getProperty("user.dir") + "/config/" + filePath), "utf-8");
			BufferedReader br = new BufferedReader(reader);
			String line = null;
			while ((line = br.readLine()) != null) {
				datas.add(line);
			}
			br.close();
			reader.close();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(new JFrame(),
					"读取文件 " + System.getProperty("user.dir") + "/config/" + filePath + " 出错!");
			return datas;
		}
		return datas;
	}

}
